/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor;

import java.applet.AppletContext;
import java.applet.AppletStub;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.HashMap;
import java.util.Locale;
import javax.swing.JFrame;
import offset.nodes.client.view.BaseApplet;

/**
 *
 * @author Walter Lütgenau
 */
public class EditorFrame extends JFrame {
    
    class FrameAppletStub implements AppletStub {
        HashMap<String,String> parameters = new HashMap<String,String>();

        @Override
        public boolean isActive() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public URL getDocumentBase() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public URL getCodeBase() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public String getParameter(String name) {
            return parameters.get(name);
        }

        @Override
        public AppletContext getAppletContext() {
            throw new UnsupportedOperationException("Not supported yet.");
        }

        @Override
        public void appletResize(int width, int height) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
        
        public void setParameter(String name, String value) {
            parameters.put(name, value);
        }
        
    }

    Editor editor;

    public Editor getEditor() {
        return editor;
    }

    /** Creates a new instance of EditorFrame */
    public EditorFrame() throws URISyntaxException {
        editor = new Editor();
        initFrameEditor();

        editor.init();
        editor.start();
        editor.setContainer(this);
        editor.setMode(Editor.MODE_LOCAL_PAGE_EDITOR);

        add(editor);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("nodes");
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/offset/nodes/client/editor/view/resources/editor.png")).getImage());
        setLocationByPlatform(true);

        pack();
        setVisible(true);
    }
    
    protected void initFrameEditor() {
        FrameAppletStub stub = new FrameAppletStub();
        stub.setParameter(BaseApplet.PAR_LOCALE, Locale.getDefault().toString());
        
        editor.setStub(stub);
    }

    public static void main(String[] arguments) throws MalformedURLException, IOException, URISyntaxException {
        EditorFrame frame = new EditorFrame();
        for (int i = 0; i < arguments.length; i++) {
            if (arguments[i].equals("-mode")) {
                frame.getEditor().setMode(Integer.parseInt(arguments[++i]));
            } else if (arguments[i].equals("-property")) {
                String key = arguments[++i];
                String value = arguments[++i];
                frame.getEditor().getProperties().put(key, value);
            } else if (arguments[i].equals("-dir")) {
                frame.getEditor().setBase(arguments[++i]);
            } else {
                frame.getEditor().open(new URL(arguments[arguments.length - 1]));
            }

        }
    }
}
